“h5_hypeb”

Information

The example shown in this file appears at: http://www.hdfgroup.org/HDF5/Tutor/selectsimple.html (accessed 10 July 2011)

The example shown in this file is following the tutorial: http://www.hdfgroup.org/HDF5/Tutor/introductory.html

Good references for the shown HDF5 functions include:
http://www.hdfgroup.org/HDF5/doc/RM/RM_H5Front.html
http://www.hdfgroup.org/HDF5/doc/RM/

Prepared 10 July 2011 by Scot Martin for Windows 7, 64-bit, Mathematica version 8.0.1.

“c” code we seek to replicate in this example

/*************************************************************
 *  
 * This example shows how to write a subset of data (a slab) to
 * a dataset in an HDF5 file.
 *
 *************************************************************/
 
#include "hdf5.h"

#define FILE        "hype.h5"
#define DATASETNAME "IntArray" 
#define RANK  2

#define DIM0     8                 /* size of dataset */       
#define DIM1     10 

int
main (void)
{
    int         rdata[DIM0][DIM1];          /* buffer for read */
 
    hid_t       file_id, dataset_id;        /* handles */
    hid_t       dataspace_id; 

    herr_t      status;                             
   
    hsize_t     count[2];              /* size of subset in the file */
    hsize_t     offset[2];             /* subset offset in the file */
    hsize_t     stride[2];
    hsize_t     block[2];
    int         i, j;


    /*************************************************************
     * Open the file and dataset and read a selection
     *************************************************************/

    file_id = H5Fopen (FILE, H5F_ACC_RDWR, H5P_DEFAULT);
    dataset_id = H5Dopen (file_id, DATASETNAME, H5P_DEFAULT);


    /*************************************************************
     * Read data using block and stride in selection  
     *************************************************************/
    status = H5Dread (dataset_id, H5T_NATIVE_INT, H5S_ALL, H5S_ALL,
                       H5P_DEFAULT, rdata);

    printf ("\nData In File :\n");
    for (i = 0; i<DIM0; i++){ 
       for (j = 0; j<DIM1; j++) 
           printf (" %i", rdata[i][j]);
       printf ("\n");
    } 

    offset[0] = 1;
    offset[1] = 1; 

    count[0]  = 2;  
    count[1]  = 2;

    stride[0] = 4;
    stride[1] = 4;

    block [0] = 2;
    block [1] = 2;


    for (i = 0; i<DIM0; i++)
       for (j = 0; j<DIM1; j++) 
           rdata[i][j]= 0;

    dataspace_id = H5Dget_space (dataset_id);
    status = H5Sselect_hyperslab (dataspace_id, H5S_SELECT_SET, offset,
                                  stride, count, block); 

    status = H5Dread (dataset_id, H5T_NATIVE_INT, H5S_ALL, dataspace_id,  
                      H5P_DEFAULT, rdata); 
  
    printf ("\nSelected Data Read From File:\n");
    for (i = 0; i<DIM0; i++){ 
       for (j = 0; j<DIM1; j++) 
           printf (" %i", rdata[i][j]);
       printf ("\n");
    }
    

    status = H5Sclose (dataspace_id);
    status = H5Dclose (dataset_id);
    status = H5Fclose (file_id);
 
}

Implementation in Mathematica

set up

SetDirectory[NotebookDirectory[]]

Needs["HDF5`"]

dims={8,10}

{offset,count,stride,block}={{1,1},{2,2},{4,4},{2,2}}

file=FileNameJoin[{Directory[],"hype.h5"}]

file and dataSet setup

fileID=H5Fopen[file,H5FuACCuRDWR,H5PuDEFAULT]

dataSetID=H5Dopen[fileID,"IntArray",H5PuDEFAULT]

read all data

readDataObject=NETNew["System.Int32[]",Times@@dims]

H5Dread[Integer32][dataSetID,H5T`copy[H5TuNATIVEuINT]@Id[],H5SuALL,H5SuALL,H5PuDEFAULT,readDataObject]

Partition[NETObjectToExpression@readDataObject,Rest@dims]

%//TableForm

read some data

dataSpaceID=H5DgetSpace[dataSetID]

H5SselectHyperSlab [dataSpaceID, H5SuSET, offset,stride, count, block]

readDataObject=NETNew["System.Int32[]",Times@@dims]

H5Dread[Integer32][dataSetID,H5T`copy[H5TuNATIVEuINT]@Id[],H5SuALL,dataSpaceID,H5PuDEFAULT,readDataObject]

Partition[NETObjectToExpression@readDataObject,Rest@dims]

%//TableForm

Close items

H5Sclose[dataSpaceID]
H5Dclose[dataSetID]
H5Fclose[fileID]